let touchStatus = false;   //滑动放触
Component({
    data: {
        startX: 0,
        activeIndex: 0,  //显示活跃下标
        translateX: null,
        ltranslateX:0,
        translateX1: 0,
        translateX2: 0,
        translateX3: 0,
        opacity1: 1,
        opacity2: 1,
        opacity3: 1,
        touchStatus:false,
    },
    props: {
      baseUrl:'',
        couponData: [],
        onMoveSwiper: null,
        initActiveIndex: 0
    },
    didUpdate () { },
    async didMount () {
        this.initData()
        $global.mfutil.emitter.on('wood_card_app_show', () => {
          // this.initData()
      })
        $global.mfutil.emitter.on('moveTimeSwiper', (params) => {
            this.moveSwiper(params)
            console.log('moveCouponSwiper')
        })
    },
    methods: {
        initData () {
            this.setData({
                activeIndex: this.props.initActiveIndex,
            })
            this.moveSwiper({nextactiveIndex:this.props.initActiveIndex})
        },
        clickTime (event) {
          console.log('clickTime')
            let { activeIndex,ltranslateX } = this.data;
            const {couponData} = this.props;
            let { index } = event.target.dataset;
            console.log(parseInt((ltranslateX-2) / 78),'====parseInt((ltranslateX-2) / 78)')
            if(index == 'left'){
              if(parseInt((ltranslateX-2) / 78) == 1 && activeIndex- 1 >= 0){
                index = activeIndex- 1;
               this.props.onMoveSwiper({ nextactiveIndex: index})
              }else if(parseInt((ltranslateX-2) / 78) == 2 && activeIndex- 2 >= 0){
                index = activeIndex - 2;
               this.props.onMoveSwiper({ nextactiveIndex: index})
              }else if(activeIndex - 3 >= 0){
                index = activeIndex -3;
                this.props.onMoveSwiper({ nextactiveIndex: index})
              }
              console.log('lefttmove')
            }
            if(index == 'right'){
              if(parseInt((ltranslateX-2) / 78) == 0 && activeIndex + 3 <= couponData.length-1){
                index = activeIndex + 3;
                this.props.onMoveSwiper({ nextactiveIndex: index})
              }else if(parseInt((ltranslateX-2) / 78) == 1 && activeIndex + 2 <= couponData.length-1){
                index = activeIndex + 2;
                this.props.onMoveSwiper({ nextactiveIndex: index})
              }else if(activeIndex + 1 <= couponData.length-1){
                index = activeIndex + 1;
                this.props.onMoveSwiper({ nextactiveIndex: index})
              }
              console.log('rightmove')
            }else if(index !== 'left' && index !== 'right'){
              this.props.onMoveSwiper({ nextactiveIndex: +index})
              console.log('indextmove')
            }
            // if(index == 0)return
            // console.log(activeIndex + +index)
            // if (activeIndex >= couponData.length - 2 && activeIndex + +index > couponData.length - 1  && +index > 0) {
            //     activeIndex = +index - (couponData.length- 1 - activeIndex) -1;
            //     console.log(111,activeIndex)
            // } else if (activeIndex == 0 && +index < 0) {
            //     activeIndex = couponData.length - 1;
            //     console.log(2222)
            // } else {
            //   console.log(333)
            //     activeIndex += +index;
            // }
        },
        onHorizontalSwipe(e){
          if (touchStatus) {
            return;
          }
          touchStatus = true;
          let { couponData } = this.props;
          let { activeIndex } = this.data;
          // let direction = 1;
          if (e.detail.direction == 'left' && activeIndex < couponData.length-1) {
            // direction = 1;
            activeIndex +=1;
          }
          if (e.detail.direction == 'right'&& activeIndex>0) {
            // direction = -1;
            activeIndex -=1;
          }
        //   if (activeIndex == couponData.length - 1 && direction == 1) {
        //     activeIndex = 0;
        // } else if (activeIndex == 0 && direction == -1) {
        //     activeIndex = couponData.length - 1;
        // } else {
        //     activeIndex += direction;
        // }
        this.props.onMoveSwiper({ nextactiveIndex: activeIndex})
        touchStatus = false;
        },
        moveSwiper (params) {
            const { nextactiveIndex = 0} = params;
            let { activeIndex,translateX, ltranslateX,translateX1, translateX2, translateX3, opacity1, opacity2, opacity3 } = this.data;
            let { couponData } = this.props;
            console.log(nextactiveIndex,'=====nextactiveIndex')
            if(nextactiveIndex<=1){
              translateX = 0;
            }else if(nextactiveIndex<couponData.length-2){
              translateX = -(nextactiveIndex-1) * 80;
            }
            if(nextactiveIndex == 0 || nextactiveIndex == 1){
              ltranslateX = nextactiveIndex * 80 
            }else if(nextactiveIndex == couponData.length-2){
              ltranslateX = 2 * 80 
              translateX = -(nextactiveIndex-2) * 80
            }else if(nextactiveIndex == couponData.length-1){
              ltranslateX = 3 * 80 
              translateX = -(couponData.length - 4) * 80 - 19; 
              ltranslateX -= 19;
            }else{
              ltranslateX = 80
            }
            
            // translateX = nextactiveIndex > 0  -nextactiveIndex * 78 + 78
            // if(nextactiveIndex < activeIndex && direction == 1){
            //   translateX -= (couponData.length - 1 - activeIndex + nextactiveIndex+1)* 90;
            // }else if(activeIndex == couponData.length - 1 && nextactiveIndex == 0 ){
            //   translateX += 90;
            // }else if(activeIndex == 0 && nextactiveIndex == couponData.length - 1){
            //   translateX += 90;
            // }else{
            //   translateX -= (nextactiveIndex - activeIndex) * 90;
            // }
            // if (translateX < - couponData.length * 90) {
            //     translateX1 = parseInt((-translateX + couponData.length * 90) / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     translateX2 = - parseInt(translateX / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     translateX3 = parseInt((-translateX - couponData.length * 90) / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     opacity1 = translateX1 - Math.abs(translateX) == couponData.length * 90 || Math.abs(translateX) - translateX1 == couponData.length * 90 * 2 - 90 ? 0 : 1;
            //     opacity2 = Math.abs(translateX2 - Math.abs(translateX)) != 0 && Math.abs(translateX2 - Math.abs(translateX)) <= couponData.length * 90 * 2 ? 1 : 0;
            //     opacity3 = Math.abs(translateX3 - Math.abs(translateX)) == couponData.length * 90 * 4 - 90 || Math.abs(translateX3 - Math.abs(translateX)) == couponData.length * 90 ? 0 : 1;
            // } else {
            //     translateX1 = - parseInt((translateX + 90 + couponData.length * 90) / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     translateX2 = - parseInt((translateX + 90 + (couponData.length * 90 * 2)) / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     translateX3 = - parseInt((translateX + 90 + couponData.length * 90 * 3) / (couponData.length * 90 * 3)) * couponData.length * 90 * 3;
            //     opacity1 = Math.abs(translateX1) - translateX - 90 == couponData.length * 90 || Math.abs(Math.abs(translateX) - Math.abs(translateX1)) == couponData.length * 90 * 2 - 90 * 2 ? 0 : 1;
            //     opacity2 = Math.abs(translateX2) - translateX - 90 == couponData.length * 90 * 2 || (translateX > 0 && translateX - Math.abs(translateX2) == couponData.length * 90 - 90 * 2) ? 0 : 1;
            //     opacity3 = Math.abs(translateX3) - translateX - 90 == couponData.length * 90 * 3 || Math.abs(Math.abs(translateX) - Math.abs(translateX3)) == 90 * 2 ? 0 : 1;
            // }
            this.setData({
                activeIndex: nextactiveIndex,
                ltranslateX,
                translateX,
                // translateX1,
                // translateX2,
                // translateX3,
                // opacity1,
                // opacity2,
                // opacity3,
                touchStatus:true
            })
        },
       
      //   touchStart (event) {
      //     if (touchStatus) return
      //     touchStatus = true;
      //     const { clientX } = event.changedTouches[0];
      //     this.data.startX = clientX;
      // },
      // touchEnd (event) {
      //     const { clientX } = event.changedTouches[0];
      //     let { startX, activeIndex } = this.data;
      //     let { couponData } = this.props;
      //     // let direction = 1;
      //     if (Math.abs(clientX - startX) < 20) return
      //     // 向右滑动
      //     if (clientX - startX > 0 && clientX - startX > 10) {
      //         // direction = -1;
      //         activeIndex -= 1;
      //     }
      //     // 向左滑动
      //     if (clientX - startX < 0 && clientX - startX < 10) {
      //         // direction = 1;
      //         activeIndex += 1;
      //     }
      //     // if (activeIndex == couponData.length - 1 && direction == 1) {
      //     //     activeIndex = 0;
      //     // } else if (activeIndex == 0 && direction == -1) {
      //     //     activeIndex = couponData.length - 1;
      //     // } else {
      //     //     activeIndex += direction;
      //     // }
      //     this.props.onMoveSwiper({ nextactiveIndex: activeIndex})
      //     touchStatus = false;
      // },
    }
})
